import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;

public class UdpClient {
    private String ServicerIP;
    private int ServicerPort;
    private DatagramSocket socket=null;
    public UdpClient(String servicerIP,int servicerPort) throws IOException {
        this.ServicerIP=servicerIP;
        this.ServicerPort=servicerPort;
        socket=new DatagramSocket();
    }
    public void start() throws IOException {
        System.out.println("客户端启动");
        Scanner scanner=new Scanner(System.in);
        while(true){
            System.out.println("->");
            String request=scanner.nextLine();
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(this.ServicerIP),this.ServicerPort);
            socket.send(requestPacket);
            DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(responsePacket);
            String response=new String(responsePacket.getData(),0,responsePacket.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        UdpClient client=new UdpClient("127.0.0.1",9090);
        client.start();
    }
}
